package com.cari.oa.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.SysDictGroupEntity;
import com.cari.oa.service.SysDictGroupService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.Utils;

/**
 * 系统字典组controller
 * User: yexuhui
 * Date: 2012-12-27
 * Time: 下午2:35:41
 */
@Controller
@SessionAttributes("username")
public class SysDictGroupController {

	@Resource
	private SysDictGroupService sysDictGroupService;
	
	public static final String MODULE_NAME = "SYS_DICTGROUP_MANAGE";
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
    
    @RequestMapping("/system/dictgroup/list.do")
    public String list(String enname, String alias, Integer status, Integer curPage, ModelMap modelMap) {
        if(curPage == null) curPage = 1;
        long totalRecords = sysDictGroupService.getCount(enname, alias, status);
        long totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
        
        List<SysDictGroupEntity> groupList = sysDictGroupService.getPage(enname, alias, status, curPage, Contants.PAGE_SIZE);

        modelMap.addAttribute("groupList", groupList);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);

        modelMap.addAttribute("enname", enname);
        modelMap.addAttribute("alias", alias);
        modelMap.addAttribute("status", status);

        return "system/dictgroup/list";
    }  
    
    @RequestMapping(value = "/system/dictgroup/insert.do", method = RequestMethod.GET)
    public String enterInsert(String qs, ModelMap modelMap) {
        modelMap.addAttribute("qs", qs);
        return "system/dictgroup/insert";
    }
    
    @RequestMapping(value = "/system/dictgroup/edit.do", method = RequestMethod.GET)
    public String enterEdit(String id, String qs, ModelMap modelMap) {
    	SysDictGroupEntity group = sysDictGroupService.getById(id);
        modelMap.addAttribute("group", group);
    	modelMap.addAttribute("qs", qs);
        return "system/dictgroup/edit";
    } 
    
    @RequestMapping(value = "/system/dictgroup/insert.do", method = RequestMethod.POST)
    @ResponseBody
    public String postInsert(@ModelAttribute("username") String username, 
    		@Valid SysDictGroupEntity group, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());

        if (sysDictGroupService.existByEnname(group.getEnname()))
            return Utils.errorJsonResp("系统字典组英文名已存在");

        sysDictGroupService.insert(group);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "新增字典组" + group.getEnname());
        return Utils.normalJsonResp();
    } 
    
    @RequestMapping(value = "/system/dictgroup/edit.do", method = RequestMethod.POST)
    @ResponseBody
    public String postEdit(@ModelAttribute("username") String username, 
    		@Valid SysDictGroupEntity group, BindingResult result) {
    	if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());
        
    	sysDictGroupService.update(group);
    	
    	SysOperLogController.addOperLog(username, MODULE_NAME, "修改字典组" + group.getEnname());
        return Utils.normalJsonResp();
    }
    
    @RequestMapping(value = "/system/dictgroup/delete.do")
    @ResponseBody
    public String delete(@ModelAttribute("username") String username, String id) {
    	sysDictGroupService.deactivate(id);
    	SysOperLogController.addOperLog(username, MODULE_NAME, "删除字典组，id" + id);
    	return Utils.normalJsonResp();
    }
}
